﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework;
using OLTS.DAL;
using OLTS.MODEL;

namespace OLTS.DATA_SERVICE
{
    public class Employee_Service
    {
        private OTS_EMPLOYEE _objApp = new OTS_EMPLOYEE();
        private String _idEmp;
        public Employee_Service(OTS_EMPLOYEE objApp)
        {
            _objApp = objApp;
        }
        public Employee_Service()
        {

        }
        public Employee_Service(String Empid)
        {
            _idEmp = Empid;
        }

        public List<OTS_EMPLOYEE> GetAll()
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_EMPLOYEE.Where(x=> x.WORKSTATUS == true ).ToList();
                return list;
            }
        }

        public OTS_EMPLOYEE GetByEmpId()
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_EMPLOYEE.Include("OTS_EDUCATION").Include("OTS_FAMILY").Include("OTS_EMERGENCY_NOTIFY").Include("OTS_MARITAL_STATUS").Where(x => x.ID == _idEmp && x.WORKSTATUS == true).FirstOrDefault();
                return list;
            }
        }

        public OTS_EMPLOYEE GetByEmpId(string id)
        {
            using (var ctx = new Entities())
            {
                var obj = ctx.OTS_EMPLOYEE.Where(x => x.ID == id).FirstOrDefault();
                return obj;  
            }
        } 
        public List<OTS_EMPLOYEE> GetAllinclude()
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_EMPLOYEE.Include("OTS_SITE").ToList();
                return list;
            }
        }

        public void Edit()
        {
            using (var ctx = new Entities())
            {
                var emp = ctx.OTS_EMPLOYEE.Include("OTS_EDUCATION").Where(x => x.ID == _objApp.ID).FirstOrDefault();
                if (emp != null)
                {
                    emp.TITLE = _objApp.TITLE;
                    emp.NAME_EN = _objApp.NAME_EN;
                    emp.NAME_TH = _objApp.NAME_TH;
                    emp.NICK_NAME = _objApp.NICK_NAME;
                    emp.GENDER = _objApp.GENDER;
                    emp.ID_CARD = _objApp.ID_CARD;
                    emp.DATEOFBIRTH = _objApp.DATEOFBIRTH;
                    emp.NATIONALITY = _objApp.NATIONALITY;
                    emp.RELIGION = _objApp.RELIGION;
                    emp.STARTWORK = _objApp.STARTWORK;
                    emp.ENDWORK = _objApp.ENDWORK;
                    emp.POSITION = _objApp.POSITION;
                    emp.DEPARTMENT = _objApp.DEPARTMENT;
                    emp.PERMANENT_ADDRESS = _objApp.PERMANENT_ADDRESS;
                    emp.PRESENT_ADDRESS = _objApp.PRESENT_ADDRESS;
                    emp.HOME = _objApp.HOME;
                    emp.EMAIL = _objApp.EMAIL;
                    emp.MOBILE = _objApp.MOBILE;
                    emp.TERMINATED_DATE = _objApp.TERMINATED_DATE;
                    emp.PROBATION_DATE = _objApp.PROBATION_DATE;
                    emp.IQ_TEST = _objApp.IQ_TEST;
                    emp.TYPE_MOS = _objApp.TYPE_MOS;
                    emp.TYPE_LEAST = _objApp.TYPE_LEAST;
                    emp.TYPE = _objApp.TYPE;
                    emp.ONSITE_OFFICE = _objApp.ONSITE_OFFICE;

                    var educreate = _objApp.OTS_EDUCATION.Where(x => x.Action == ActionEnum.Create).ToList();
                    var eduupdate = _objApp.OTS_EDUCATION.Where(x => x.Action == ActionEnum.Update).ToList();
                    var cmd = new Education_Service();
                    if (educreate.Count > 0)
                    {
                        cmd.Add(educreate);
                    }
                    if (eduupdate.Count > 0)
                    {
                        cmd.Edit(eduupdate);
                    }

                    var famcreate = _objApp.OTS_FAMILY.Where(x => x.Action == ActionEnum.Create).ToList();
                    var famupdate = _objApp.OTS_FAMILY.Where(x => x.Action == ActionEnum.Update).ToList();
                    var famcmd = new FAMILY_Service();
                    if (famcreate.Count > 0)
                    {
                        famcmd.Add(famcreate);
                    }
                    if (famupdate.Count > 0)
                    {
                        famcmd.Edit(famupdate);
                    }

                    var marcreate = _objApp.OTS_MARITAL_STATUS.Where(x => x.Action == ActionEnum.Create).ToList();
                    var marupdate = _objApp.OTS_MARITAL_STATUS.Where(x => x.Action == ActionEnum.Update).ToList();
                    var marcmd = new MARITAL_STATUS_Service();
                    if (marcreate.Count > 0)
                    {
                        marcmd.Add(marcreate);
                    }
                    if (marupdate.Count > 0)
                    {
                        marcmd.Edit(marupdate);
                    }

                    var emercreate = _objApp.OTS_EMERGENCY_NOTIFY.Where(x => x.Action == ActionEnum.Create).ToList();
                    var emerupdate = _objApp.OTS_EMERGENCY_NOTIFY.Where(x => x.Action == ActionEnum.Update).ToList();
                    var emercmd = new EMERGENCY_NOTIFY_Service();
                    if (emercreate.Count > 0)
                    {
                        emercmd.Add(emercreate);
                    }
                    if (emerupdate.Count > 0)
                    {
                        emercmd.Edit(emerupdate);
                    }
                    ctx.SaveChanges();
                }
            }
        }

        public void Add()
        {
            using (var ctx = new Entities())
            {
                _objApp.CREATED = DateTime.Now;
                ctx.OTS_EMPLOYEE.AddObject(_objApp);
                ctx.SaveChanges();
            }
        }

        public void Delete(string id)
        {
            using (var ctx = new Entities())
            {
                var obj = ctx.OTS_EMPLOYEE.Where(x => x.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    obj.WORKSTATUS = false;
                    //ctx.OTS_EMPLOYEE.DeleteObject(obj);
                    ctx.SaveChanges();
                }

            }
        }

    }
}
